<template>
	<view class="content">
		<!-- <view class="type_div"  @click="selCourse">
			<text style="color: #666666;">选择课程</text>
			<text>{{currCourse}}</text>
			<image src="../../static/sobot_right_arrow_icon.png"></image>
		</view> -->
		<view class="page-body">
			<editor id="editor" class="ql-container" placeholder="开始输入..." showImgSize showImgToolbar showImgResize
				@statuschange="onStatusChange" :read-only="readOnly" @ready="onEditorReady"
				 @input="getText"  >
			</editor>
		</view>


		<view class='toolbar' @tap="format">
			<view :class="formats.bold ? 'ql-active' : ''" class="iconfont icon-zitijiacu" data-name="bold">
			</view>
			<view :class="formats.italic ? 'ql-active' : ''" class="iconfont icon-zitixieti" data-name="italic">
			</view>
			<view :class="formats.underline ? 'ql-active' : ''" class="iconfont icon-zitixiahuaxian"
				data-name="underline"></view>
			<view :class="formats.strike ? 'ql-active' : ''" class="iconfont icon-zitishanchuxian" data-name="strike">
			</view>
			<view :class="formats.align === 'left' ? 'ql-active' : ''" class="iconfont icon-zuoduiqi" data-name="align"
				data-value="left"></view>
			<view :class="formats.align === 'center' ? 'ql-active' : ''" class="iconfont icon-juzhongduiqi"
				data-name="align" data-value="center"></view>
			<view :class="formats.align === 'right' ? 'ql-active' : ''" class="iconfont icon-youduiqi" data-name="align"
				data-value="right"></view>
			<view :class="formats.align === 'justify' ? 'ql-active' : ''" class="iconfont icon-zuoyouduiqi"
				data-name="align" data-value="justify"></view>
			<view :class="formats.lineHeight ? 'ql-active' : ''" class="iconfont icon-line-height"
				data-name="lineHeight" data-value="2"></view>
			<view :class="formats.letterSpacing ? 'ql-active' : ''" class="iconfont icon-Character-Spacing"
				data-name="letterSpacing" data-value="2em"></view>
			<view :class="formats.marginTop ? 'ql-active' : ''" class="iconfont icon-722bianjiqi_duanqianju"
				data-name="marginTop" data-value="20px"></view>
			<view :class="formats.previewarginBottom ? 'ql-active' : ''" class="iconfont icon-723bianjiqi_duanhouju"
				data-name="marginBottom" data-value="20px"></view>
			<view class="iconfont icon-clearedformat" @tap="removeFormat"></view>
			<view :class="formats.fontFamily ? 'ql-active' : ''" class="iconfont icon-font" data-name="fontFamily"
				data-value="Pacifico"></view>
			<view :class="formats.fontSize === '24px' ? 'ql-active' : ''" class="iconfont icon-fontsize"
				data-name="fontSize" data-value="24px"></view>

			<view :class="formats.color === '#0000ff' ? 'ql-active' : ''" class="iconfont icon-text_color"
				data-name="color" data-value="#0000ff"></view>
			<view :class="formats.backgroundColor === '#00ff00' ? 'ql-active' : ''" class="iconfont icon-fontbgcolor"
				data-name="backgroundColor" data-value="#00ff00"></view>

			<view class="iconfont icon-date" @tap="insertDate"></view>
			<view class="iconfont icon--checklist" data-name="list" data-value="check"></view>
			<view :class="formats.list === 'ordered' ? 'ql-active' : ''" class="iconfont icon-youxupailie"
				data-name="list" data-value="ordered"></view>
			<view :class="formats.list === 'bullet' ? 'ql-active' : ''" class="iconfont icon-wuxupailie"
				data-name="list" data-value="bullet"></view>
			<view class="iconfont icon-undo" @tap="undo"></view>
			<view class="iconfont icon-redo" @tap="redo"></view>

			<view class="iconfont icon-outdent" data-name="indent" data-value="-1"></view>
			<view class="iconfont icon-indent" data-name="indent" data-value="+1"></view>
			<view class="iconfont icon-fengexian" @tap="insertDivider"></view>
			<view class="iconfont icon-charutupian" @tap="insertImage"></view>
			<view :class="formats.header === 1 ? 'ql-active' : ''" class="iconfont icon-format-header-1"
				data-name="header" :data-value="1"></view>
			<view :class="formats.script === 'sub' ? 'ql-active' : ''" class="iconfont icon-zitixiabiao"
				data-name="script" data-value="sub"></view>
			<view :class="formats.script === 'super' ? 'ql-active' : ''" class="iconfont icon-zitishangbiao"
				data-name="script" data-value="super"></view>
			<view class="iconfont icon-shanchu" @tap="clear"></view>
			<view :class="formats.direction === 'rtl' ? 'ql-active' : ''" class="iconfont icon-direction-rtl"
				data-name="direction" data-value="rtl"></view>

		</view>

		<view class="sub_div" @click="sub">提交</view>
		<popCourse v-show="bottomSHow" :courseList="courseList" @courseItemClicks="courseItem"></popCourse>
		
	</view>
</template>

<script>
	import popCourse from '../../components/course/course.vue';
	export default {
		components: {
			popCourse
		},
		data() {
			return {
				id:0,//题目Id
				readOnly: false,
				formats: {},
				/* type: "阿里云ACA", */
				htmlText:"",
				/* bottomSHow: false, //是否显示选择课程
				currCourse: "阿里云ACA", //当前课程
				courseList: [{
					"id": 1,
					name: "阿里云ACA"
				}, {
					"id": 2,
					name: "DP900"
				}], */
			}
		},
		methods: {
		/* 	courseItem: function(index) {
				//点击某个科目
				this.currCourse = this.courseList[index].name;
				this.bottomSHow = false;
			},
			selCourse: function() {
				//点击切换科目
				this.bottomSHow = true;
			}, */
			sub: function() {
				//提交笔记
				uni.request({
					url: this.baseUrl + 'studentNote/insert',
					method:'POST',
					data:{
						'stuId':uni.getStorageSync('uid'),
						'topicId':parseInt(this.id),
						'content':this.htmlText
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded' //自定义请求头信息
					},
					success:function(res){
						
						if(res.data.code==0){
							uni.navigateBack();
						}
					}
				})
				
			},
			getText:function(e){
				this.htmlText=e.detail.html;
			},
			readOnlyChange() {
				this.readOnly = !this.readOnly
			},
			onEditorReady() {
				uni.createSelectorQuery().select('#editor').context((res) => {
					this.editorCtx = res.context
				}).exec()
			},
			undo() {
				this.editorCtx.undo()
			},
			redo() {
				this.editorCtx.redo()
			},
			format(e) {
				let {
					name,
					value
				} = e.target.dataset
				if (!name) return
				// console.log('format', name, value)
				this.editorCtx.format(name, value)

			},
			onStatusChange(e) {
				const formats = e.detail
				this.formats = formats
			},
			insertDivider() {
				this.editorCtx.insertDivider({
					success: function() {
						console.log('insert divider success')
					}
				})
			},
			clear() {
				this.editorCtx.clear({
					success: function(res) {
						console.log("clear success")
					}
				})
			},
			removeFormat() {
				this.editorCtx.removeFormat()
			},
			insertDate() {
				const date = new Date()
				const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
				this.editorCtx.insertText({
					text: formatDate
				})
			},
			insertImage() {
				uni.chooseImage({
					count: 1,
					success: (res) => {
						this.editorCtx.insertImage({
							src: res.tempFilePaths[0],
							alt: '图像',
							success: function() {
								console.log('insert image success')
							}
						})
					}
				})
			}
		},
		onLoad(option) {
			this.id=option.id;
			uni.loadFontFace({
				family: 'Pacifico',
				source: 'url("https://sungd.github.io/Pacifico.ttf")'
			})
		},
	}
</script>

<style>
	@import "./editor-icon.css";

	page {
		height: 100%;
		background: #FFFFFF;
	}


	/* .type_div {
		background: #FFFFFF;
		height: 100rpx;
		line-height: 100rpx;
		display: flex;
		justify-content: space-between;
		font-size: 28rpx;
		color: #333333;
		padding-left: 20rpx;
		padding-right: 20rpx;
		align-items: center;

	}

	.type_div text:nth-child(2) {
		flex: 1;
		margin-left: 20rpx;
	}

	.type_div image {
		width: 16rpx;
		height: 20rpx;
	}

 */

	.page-body {	
		background: #fff;
		position: absolute;
		top: 0rpx;
		bottom: 100rpx;
		left: 0;
		right: 0;
		border-top: 20rpx solid #f2f2f2;

	}

	.iconfont {
		display: inline-block;
		padding: 16rpx 16rpx;
		width: 48rpx;
		height: 48rpx;
		cursor: pointer;
		font-size: 40rpx;
	}

	.toolbar {
		box-sizing: border-box;
		border-bottom: 0;
		font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
		display: flex;
		height: 100rpx;
		align-items: center;
		overflow-y: auto;
		position: fixed;
		bottom: 0;
		background: #F8F8F8;
		width: 100%;
		border-top: 1rpx solid #DADADA;
	}


	.ql-container {
		box-sizing: border-box;
		padding: 20rpx;
		width: 100%;
		min-height: 30vh;
		font-size: 16px;
		line-height: 1.5;
		height: 100%;
	}

	.ql-active {
		color: #06c;
	}

	.sub_div {
		width: 100rpx;
		height: 100rpx;
		border-radius: 50rpx;
		background: #e13b29;
		position: fixed;
		right: 40rpx;
		bottom: 140rpx;
		text-align: center;
		line-height: 100rpx;
		color: #FFFFFF;
		font-size: 28rpx;
		box-shadow: 5rpx 5rpx 5rpx #888888;
	}
</style>
